package scau.redi.social_platform.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scau.redi.social_platform.common.staticCode.Const;
import scau.redi.social_platform.common.util.ResponsePageUtil;
import scau.redi.social_platform.dao.SharedMapper;
import scau.redi.social_platform.dao.UserInfoMapper;
import scau.redi.social_platform.domain.pojo.Shared;
import scau.redi.social_platform.domain.pojo.UserInfo;
import scau.redi.social_platform.service.AdminService;

import java.util.List;

/**
 * @Author: Redi
 * @Date: 2022/5/10 10:19
 * @Description:
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    SharedMapper sharedMapper;

    @Override
    public int banUser(UserInfo userInfo) {
        int updateResultCode = userInfoMapper.updateByPrimaryKeySelective(userInfo);
        if (updateResultCode == 0 ){
            return 0;
        }
        List<Shared> sharedList = sharedMapper.selectByUserId(userInfo.getId());
        int errorUpdateResultSum = 0;    // 所有更新结果的flag
        for (Shared changedShared:sharedList){
            changedShared.setStatusId(Const.SharedStatus.STATUS_INVISIBLE);
            int updateCode = sharedMapper.updateByPrimaryKeySelective(changedShared);
            if (updateCode == 0){
                errorUpdateResultSum++;
            }
        }
        if (errorUpdateResultSum == 0){
            return 1;
        }
        else {
            return 0;
        }
    }

    @Override
    public int unbanUser(UserInfo userInfo) {
        int updateResultCode = userInfoMapper.updateByPrimaryKeySelective(userInfo);
        if (updateResultCode == 0 ){
            return 0;
        }
        List<Shared> sharedList = sharedMapper.selectByUserId(userInfo.getId());
        int errorUpdateResultSum = 0;    // 所有更新结果的flag
        for (Shared changedShared:sharedList){
            changedShared.setStatusId(Const.SharedStatus.STATUS_NORMAL);
            int updateCode = sharedMapper.updateByPrimaryKeySelective(changedShared);
            if (updateCode == 0){
                errorUpdateResultSum++;
            }
        }
        if (errorUpdateResultSum == 0){
            return 1;
        }
        else {
            return 0;
        }
    }

    @Override
    public ResponsePageUtil<UserInfo> findAllUser(int page, int size) {
        PageHelper.startPage(page, size);
        List<UserInfo> users = userInfoMapper.selectAllUser();

        // 把分页的list传入（包含分页信息），后面再把data内容换为VOList
        ResponsePageUtil<UserInfo> sharedWithContentResponsePageUtil = ResponsePageUtil.restPage(users);

        ResponsePageUtil<UserInfo> pageUtil = new ResponsePageUtil<>(
                page,
                size,
                sharedWithContentResponsePageUtil.getTotalPage(),
                (int) sharedWithContentResponsePageUtil.getTotalItemNum(),
                users);
        return pageUtil;
    }
}
